<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use Intervention\Image\ImageManagerStatic as Image;//缩放
class MaintainController extends Controller
{
	//更改站点
    public function edit()
    {
        $data = DB::table('websize') -> get();
        return view('/admin/website/maintain/edit',['data' => $data]);
    }

    //处理更改
    public function update(Request $request)
    {    	

        $data = $request -> except('_token','oldlog'); 
        //处理图片
        if($request ->hasFile('log'))
        {
            if($request -> file('log') -> isValid())
            {
                //获取后缀名
                $suffix = $request -> file('log') -> getClientOriginalExtension();
                // echo $suffix;

                //随机文件名称
                $fileName = time().mt_rand(100000,999999).'.'.$suffix;
                
                $data['log'] = '/uploads/website/log/'.$fileName;
                // dd($data);
                //将文件移入指定文件夹中
                $request -> file('log') -> move('./uploads/website/log/',$fileName); 
                
                $oldlog = '.'.$request -> input('oldlog');
                // dd($oldlog);
                //更新的同时删除旧图片
                if($oldlog != '.')
                {
                    if(file_exists($oldlog))
                    {
                        unlink($oldlog);//删除旧图片
                    }
                }
               
            }

        }

        $res = DB::table('websize') -> update($data);
        if($res)
        {
        	echo '<script>alert("更新成功");window.location.href="edit"</script>';
        }else
        {
        	echo '<script>alert("更新失败");window.location.href="edit"</script>';
        }


    }

    //网站开关
    public function errors()
    {
    	$data = DB::table('websize') -> select('switch') -> get();
        //var_dump($data);
        if($data[0] -> switch == 1)
        {

            return redirect('/');
        }else{
           return view('/errors/503');
        }
    	
    }
}